home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / dabbrev.el.z / dabbrev.el
Encoding:
Text File  |  1998-05-21  |  33.9 KB  |  927 lines

  1. ;;; dabbrev.el --- dynamic abbreviation package
  2. ;; Copyright (C) 1985, 1986, 1992, 1994 Free Software Foundation, Inc.
  3.  
  4. ;; Author: Don Morrison
  5. ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se>
  6. ;; Created: 16 Mars 1992
  7. ;; Lindberg's last update version: 5.7
  8. ;; Keywords: abbrev expand completion
  9.  
  10. ;; This file is part of XEmacs.
  11.  
  12. ;; XEmacs is free software; you can redistribute it and/or modify it
  13. ;; under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2 of the License, or
  15. ;; (at your option) any later version.
  16. ;;
  17. ;; XEmacs is distributed in the hope that it will be useful, but
  18. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20. ;; General Public License for more details.
  21. ;;
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  24. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, MA
  25. ;; 02111-1307, USA.
  26.  
  27. ;;; Synched up with: FSF 19.34.
  28.  
  29. ;;; Commentary:
  30.  
  31. ;; The purpose with this package is to let you write just a few
  32. ;; characters of words you've written earlier to be able to expand
  33. ;; them.
  34. ;;
  35. ;; To expand a word, just put the point right after the word and press
  36. ;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion).
  37. ;;
  38. ;; Check out the customizable variables below to learn about all the
  39. ;; features of this package.
  40.  
  41. ;;; Hints and tips for major modes writers:
  42.  
  43. ;; Recommended values        C/Lisp etc    text
  44. ;; dabbrev-case-fold-search    nil        t
  45. ;; dabbrev-case-replace        nil        t
  46. ;;
  47. ;; Set the variables you want special for your mode like this:
  48. ;; (set (make-local-variable 'dabbrev-case-replace) nil)
  49. ;; Then you don't interfere with other modes.
  50. ;;
  51. ;; If your mode handles buffers that refers to other buffers
  52. ;; (i.e. compilation-mode, gud-mode), then try to set
  53. ;; `dabbrev-select-buffers-function' or `dabbrev-friend-buffer-function'
  54. ;; to a function that point out those buffers.
  55.  
  56. ;; Same goes for major-modes that are connected to other modes.  There
  57. ;; are for instance a number of mail-modes.  One for reading, one for
  58. ;; creating a new mail etc.  Maybe those should be connected.
  59.  
  60. ;; Example for GNUS (when we write a reply, we want dabbrev to look in
  61. ;; the article for expansion):
  62. ;; (set (make-local-variable 'dabbrev-friend-buffer-function)
  63. ;;      (lambda (buffer)
  64. ;;         (save-excursion
  65. ;;           (set-buffer buffer)
  66. ;;           (memq major-mode '(news-reply-mode gnus-article-mode)))))
  67.  
  68.  
  69. ;; Known bugs and limitations.
  70. ;; - Possible to do several levels of `dabbrev-completion' in the
  71. ;;   minibuffer.
  72. ;; - dabbrev-completion doesn't handle resetting the globals variables
  73. ;;   right.  It resets them after finding the abbrev.
  74.  
  75. ;; Future enhancements
  76. ;;  - Check the tags-files? Like tags-complete?
  77. ;;  - Add the possibility of searching both forward and backward to
  78. ;;    the nearest expansion.
  79. ;;  - Check the kill-ring when everything else fails.  (Maybe something
  80. ;;  for hippie-expand?).  [Bng] <boris@cs.rochester.edu>
  81.  
  82. ;;; These people gave suggestions:
  83. ;;  [hymie]    Hyman Rosen <marks!hymie@jyacc.jyacc.com>
  84. ;;  [burgett]    Steve Burgett <burgett@bizet.eecs.berkeley.edu>
  85. ;;  [jules]    Julian Gosnell <jules@x.co.uk>
  86. ;;  [kifer]    Michael Kifer <kifer@sbcs.sunysb.edu>
  87. ;;  [ake]    Ake Stenhoff <extaksf@aom.ericsson.se>
  88. ;;  [alon]    Alon Albert <al%imercury@uunet.uu.net>
  89. ;;  [tromey]    Tom Tromey <tromey@busco.lanl.gov>
  90. ;;  [Rolf]    Rolf Schreiber <rolf@mathematik.uni-stuttgart.de>
  91. ;;  [Petri]    Petri Raitio <per@tekla.fi>
  92. ;;  [ejb]    Jay Berkenbilt <ejb@ERA.COM>
  93. ;;  [hawley]    Bob Hawley <rth1@quartet.mt.att.com>
  94. ;;  ... and to all the people who have participated in the beta tests.
  95.  
  96. ;;; Code:
  97.  
  98. ;;----------------------------------------------------------------
  99. ;; Customization variables
  100. ;;----------------------------------------------------------------
  101.  
  102. (defgroup dabbrev nil
  103.   "Dynamic Abbreviations"
  104.   :tag "Dynamic Abbreviations"
  105.   :group 'abbrev)
  106.  
  107. (defcustom dabbrev-backward-only nil
  108.   "*If non-nil, `dabbrev-expand' only looks backwards."
  109.   :type 'boolean
  110.   :group 'dabbrev)
  111.  
  112. (defcustom dabbrev-limit nil
  113.   "*Limits region searched by `dabbrev-expand' to this many chars away."
  114.   :type '(choice (const :tag "off" nil)
  115.          integer)
  116.   :group 'dabbrev)
  117.  
  118. (defcustom dabbrev-abbrev-skip-leading-regexp nil
  119.   "*Regexp for skipping leading characters of an abbreviation.
  120.  
  121. Example: Set this to \"\\\\$\" for programming languages
  122. in which variable names may appear with or without a leading `$'.
  123. \(For example, in Makefiles.)
  124.  
  125. Set this to nil if no characters should be skipped."
  126.   :type '(choice regexp
  127.          (const :tag "off" nil))
  128.   :group 'dabbrev)
  129.  
  130. ;; XEmacs change: The old defaults are just too obnoxious.  Rarely
  131. ;; do you actually want the case-folding behavior here, even though
  132. ;; it's useful to have case-fold-search set to t most of the time.
  133. (defcustom dabbrev-case-fold-search nil ;;'case-fold-search
  134.   "*Non-nil if dabbrev searches should ignore case.
  135. A value of nil means case is significant.
  136.  
  137. The value of this variable is an expression; it is evaluated
  138. and the resulting value determines the decision.
  139. For example: setting this to `case-fold-search' means evaluate that
  140. variable to see whether its value is nil."
  141.   :type 'sexp
  142.   :group 'dabbrev)
  143.  
  144. (defcustom dabbrev-upcase-means-case-search nil
  145.   "*The significance of an uppercase character in an abbreviation.
  146. nil means case fold search, non-nil means case sensitive search.
  147.  
  148. This variable has an effect only when the value of
  149. `dabbrev-case-fold-search' evaluates to t."
  150.   :type 'boolean
  151.   :group 'dabbrev)
  152.  
  153. ;; XEmacs change: likewise here.
  154. ;; I recommend that you set this to nil.
  155. (defcustom dabbrev-case-replace nil ;;'case-replace
  156.   "*Non-nil means dabbrev should preserve case when expanding the abbreviation.
  157. More precisely, it preserves the case pattern of the abbreviation as you
  158. typed it--as opposed to the case pattern of the expansion that is copied.
  159. The value of this variable is an expression; it is evaluated
  160. and the resulting value determines the decision.
  161. For example, setting this to `case-replace' means evaluate that
  162. variable to see if its value is t or nil.
  163.  
  164. This variable has an effect only when the value of
  165. `dabbrev-case-fold-search' evaluates to t."
  166.   :type 'sexp
  167.   :group 'dabbrev)
  168.  
  169. (defcustom dabbrev-abbrev-char-regexp nil
  170.   "*Regexp to recognize a character in an abbreviation or expansion.
  171. This regexp will be surrounded with \\\\( ... \\\\) when actually used.
  172.  
  173. Set this variable to \"\\\\sw\" if you want ordinary words or
  174. \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
  175. syntax is \"symbol\" as well as those whose syntax is \"word\".
  176.  
  177. The value nil has a special meaning: the abbreviation is from point to
  178. previous word-start, but the search is for symbols.
  179.  
  180. For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
  181. while `yes', `or', `no' and `p' are considered words.  If this
  182. variable is nil, then expanding `yes-or-no-' looks for a symbol
  183. starting with or containing `no-'.  If you set this variable to
  184. \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
  185. `yes-or-no-'.  Finally, if you set this variable to \"\\\\sw\", then
  186. expanding `yes-or-no-' signals an error because `-' is not part of a word;
  187. but expanding `yes-or-no' looks for a word starting with `no'.
  188.  
  189. The recommended value is \"\\\\sw\\\\|\\\\s_\"."
  190.   :type '(choice (const nil)
  191.          regexp)
  192.   :group 'dabbrev)
  193.  
  194. (defcustom dabbrev-check-all-buffers t
  195.   "*Non-nil means dabbrev package should search *all* buffers.
  196.  
  197. Dabbrev always searches the current buffer first.  Then, if
  198. `dabbrev-check-other-buffers' says so, it searches the buffers
  199. designated by `dabbrev-select-buffers-function'.
  200.  
  201. Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
  202. all the other buffers."
  203.   :type 'boolean
  204.   :group 'dabbrev)
  205.  
  206. (defcustom dabbrev-check-other-buffers t
  207.   "*Should \\[dabbrev-expand] look in other buffers?\
  208.  
  209. nil: Don't look in other buffers.
  210. t: Also look for expansions in the buffers pointed out by
  211.    `dabbrev-select-buffers-function'.
  212. Anything else: When we can't find any more expansions in
  213. the current buffer, then ask the user whether to look in other
  214. buffers too.
  215.  
  216. The default value is t."
  217.   :type '(choice (const :tag "off" nil)
  218.          (const :tag "on" t)
  219.          (const :tag "ask" other))
  220.   :group 'dabbrev)
  221.  
  222. ;; I guess setting this to a function that selects all C- or C++-
  223. ;; mode buffers would be a good choice for a debugging buffer,
  224. ;; when debugging C- or C++-code.
  225. (defvar dabbrev-select-buffers-function 'dabbrev--select-buffers
  226.   "A function that selects buffers that should be searched by dabbrev.
  227. The function should take no arguments and return a list of buffers to
  228. search for expansions.  Have a look at `dabbrev--select-buffers' for
  229. an example.
  230.  
  231. A mode setting this variable should make it buffer local.")
  232.  
  233. (defcustom dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p
  234.   "*A function to decide whether dabbrev should search OTHER-BUFFER.
  235. The function should take one argument, OTHER-BUFFER, and return
  236. non-nil if that buffer should be searched.  Have a look at
  237. `dabbrev--same-major-mode-p' for an example.
  238.  
  239. The value of `dabbrev-friend-buffer-function' has an effect only if
  240. the value of `dabbrev-select-buffers-function' uses it.  The function
  241. `dabbrev--select-buffers' is one function you can use here.
  242.  
  243. A mode setting this variable should make it buffer local."
  244.   :type 'function
  245.   :group 'dabbrev)
  246.  
  247. (defcustom dabbrev-search-these-buffers-only nil
  248.   "If non-nil, a list of buffers which dabbrev should search.
  249. If this variable is non-nil, dabbrev will only look in these buffers.
  250. It will not even look in the current buffer if it is not a member of
  251. this list.")
  252.  
  253. ;;----------------------------------------------------------------
  254. ;; Internal variables
  255. ;;----------------------------------------------------------------
  256.  
  257. ;; Last obarray of completions in `dabbrev-completion'
  258. (defvar dabbrev--last-obarray nil)
  259.  
  260. ;; Table of expansions seen so far
  261. (defvar dabbrev--last-table nil)
  262.  
  263. ;; Last string we tried to expand.
  264. (defvar dabbrev--last-abbreviation nil)
  265.  
  266. ;; Location last abbreviation began
  267. (defvar dabbrev--last-abbrev-location nil)
  268.  
  269. ;; Direction of last dabbrevs search
  270. (defvar dabbrev--last-direction 0)
  271.  
  272. ;; Last expansion of an abbreviation.
  273. (defvar dabbrev--last-expansion nil)
  274.  
  275. ;; Location the last expansion was found.
  276. (defvar dabbrev--last-expansion-location nil)
  277.  
  278. ;; The list of remaining buffers with the same mode as current buffer.
  279. (defvar dabbrev--friend-buffer-list nil)
  280.  
  281. ;; The buffer we looked in last.
  282. (defvar dabbrev--last-buffer nil)
  283.  
  284. ;; The buffer we found the expansion last time.
  285. (defvar dabbrev--last-buffer-found nil)
  286.  
  287. ;; The buffer we last did a completion in.
  288. (defvar dabbrev--last-completion-buffer nil)
  289.  
  290. ;; Non-nil means we should upcase
  291. ;; when copying successive words.
  292. (defvar dabbrev--last-case-pattern nil)
  293.  
  294. ;; Same as dabbrev-check-other-buffers, but is set for every expand.
  295. (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
  296.  
  297. ;; The regexp for recognizing a character in an abbreviation.
  298. (defvar dabbrev--abbrev-char-regexp nil)
  299.  
  300. ;;----------------------------------------------------------------
  301. ;; Macros
  302. ;;----------------------------------------------------------------
  303.  
  304. ;;; Get the buffer that mini-buffer was activated from
  305. (defsubst dabbrev--minibuffer-origin ()
  306.   (car (cdr (buffer-list))))
  307.  
  308. ;; Make a list of some of the elements of LIST.
  309. ;; Check each element of LIST, storing it temporarily in the
  310. ;; variable ELEMENT, and include it in the result
  311. ;; if CONDITION evaluates non-nil.
  312. (defmacro dabbrev-filter-elements (element list condition)
  313.   (` (let (dabbrev-result dabbrev-tail (, element))
  314.        (setq dabbrev-tail (, list))
  315.        (while dabbrev-tail
  316.      (setq (, element) (car dabbrev-tail))
  317.      (if (, condition)
  318.          (setq dabbrev-result (cons (, element) dabbrev-result)))
  319.      (setq dabbrev-tail (cdr dabbrev-tail)))
  320.        (nreverse dabbrev-result))))
  321.  
  322. (defun dabbrev--extent-clicked-on (event extent user-data)
  323.   (let ((buffer (first user-data))
  324.     (point (second user-data))
  325.     (init (third user-data))
  326.     (wconfig (fourth user-data)))
  327.     (set-window-configuration wconfig)
  328.     (set-buffer buffer)
  329.     (goto-char point)
  330.     (dabbrev--substitute-expansion nil init (extent-string extent))))
  331.  
  332. ;;----------------------------------------------------------------
  333. ;; Exported functions
  334. ;;----------------------------------------------------------------
  335.  
  336. ;; XEmacs changes:
  337. ;;;###autoload
  338. (define-key global-map [(meta /)] 'dabbrev-expand)
  339. ;;;??? Do we want this?
  340. ;;;###autoload
  341. (define-key global-map [(meta control /)] 'dabbrev-completion)
  342.  
  343. ;;;###autoload
  344. (defun dabbrev-completion (&optional arg)
  345.   "Completion on current word.
  346. Like \\[dabbrev-expand] but finds all expansions in the current buffer
  347. and presents suggestions for completion.
  348.  
  349. With a prefix argument, it searches all buffers accepted by the
  350. function pointed out by `dabbrev-friend-buffer-function' to find the
  351. completions.
  352.  
  353. If the prefix argument is 16 (which comes from C-u C-u),
  354. then it searches *all* buffers.
  355.  
  356. With no prefix argument, it reuses an old completion list
  357. if there is a suitable one already."
  358.  
  359.   (interactive "*P")
  360.   (dabbrev--reset-global-variables)
  361.   (let* ((dabbrev-check-other-buffers (and arg t))
  362.      (dabbrev-check-all-buffers
  363.       (and arg (= (prefix-numeric-value arg) 16)))
  364.      (abbrev (dabbrev--abbrev-at-point))
  365.      (ignore-case-p  (and (eval dabbrev-case-fold-search)
  366.                 (or (not dabbrev-upcase-means-case-search)
  367.                     (string= abbrev (downcase abbrev)))))
  368.      (my-obarray dabbrev--last-obarray)
  369.      init)
  370.     (save-excursion
  371.       (if (and (null arg)
  372.            my-obarray
  373.            (or (eq dabbrev--last-completion-buffer (current-buffer))
  374.            (and (window-minibuffer-p (selected-window))
  375.             (eq dabbrev--last-completion-buffer
  376.                 (dabbrev--minibuffer-origin))))
  377.            dabbrev--last-abbreviation
  378.            (>= (length abbrev) (length dabbrev--last-abbreviation))
  379.            (string= dabbrev--last-abbreviation
  380.             (substring abbrev 0
  381.                    (length dabbrev--last-abbreviation)))
  382.            (setq init (try-completion abbrev my-obarray)))
  383.       ;; We can reuse the existing completion list.
  384.       nil
  385.     ;;--------------------------------
  386.     ;; New abbreviation to expand.
  387.     ;;--------------------------------
  388.     (setq dabbrev--last-abbreviation abbrev)
  389.     ;; Find all expansion
  390.     (let ((completion-list
  391.            (dabbrev--find-all-expansions abbrev ignore-case-p))
  392.           (completion-ignore-case ignore-case-p))
  393.       ;; Make an obarray with all expansions
  394.       (setq my-obarray (make-vector (length completion-list) 0))
  395.       (or (> (length my-obarray) 0)
  396.           (error "No dynamic expansion for \"%s\" found%s"
  397.              abbrev
  398.              (if dabbrev--check-other-buffers "" " in this-buffer")))
  399.       (cond
  400.        ((or (not ignore-case-p)
  401.         (not dabbrev-case-replace))
  402.         (mapcar (function (lambda (string)
  403.                 (intern string my-obarray)))
  404.             completion-list))
  405.        ((string= abbrev (upcase abbrev))
  406.         (mapcar (function (lambda (string)
  407.                 (intern (upcase string) my-obarray)))
  408.             completion-list))
  409.        ((string= (substring abbrev 0 1)
  410.              (upcase (substring abbrev 0 1)))
  411.         (mapcar (function (lambda (string)
  412.                 (intern (capitalize string) my-obarray)))
  413.             completion-list))
  414.        (t
  415.         (mapcar (function (lambda (string)
  416.                 (intern (downcase string) my-obarray)))
  417.             completion-list)))
  418.       (setq dabbrev--last-obarray my-obarray)
  419.       (setq dabbrev--last-completion-buffer (current-buffer))
  420.       ;; Find the longest common string.
  421.       (setq init (try-completion abbrev my-obarray)))))
  422.     ;;--------------------------------
  423.     ;; Let the user choose between the expansions
  424.     ;;--------------------------------
  425.     (or (stringp init)
  426.     (setq init abbrev))
  427.     (cond
  428.      ;; * Replace string fragment with matched common substring completion.
  429.      ((and (not (string-equal init ""))
  430.        (not (string-equal (downcase init) (downcase abbrev))))
  431.       (if (> (length (all-completions init my-obarray)) 1)
  432.       (message "Repeat `%s' to see all completions"
  433.            (key-description (this-command-keys)))
  434.     (message "The only possible completion"))
  435.       (dabbrev--substitute-expansion nil abbrev init))
  436.      (t
  437.       ;; * String is a common substring completion already.  Make list.
  438.       (message "Making completion list...")
  439.       ;; construct the arg before calling `with-output-to-temp-buffer'
  440.       ;; because that changes the window config
  441.       (let ((arg (list (current-buffer)
  442.                (set-marker (make-marker) (point))
  443.                init
  444.                (current-window-configuration))))
  445.     (with-output-to-temp-buffer " *Completions*"
  446.       (display-completion-list (all-completions init my-obarray)
  447.                    :activate-callback
  448.                    'dabbrev--extent-clicked-on
  449.                    :user-data arg)))
  450.       (message "Making completion list...done")))
  451.     (and (window-minibuffer-p (selected-window))
  452.      (message nil))))
  453.  
  454. ;;;###autoload
  455. (defun dabbrev-expand (arg)
  456.   "Expand previous word \"dynamically\".
  457.  
  458. Expands to the most recent, preceding word for which this is a prefix.
  459. If no suitable preceding word is found, words following point are
  460. considered.  If still no suitable word is found, then look in the
  461. buffers accepted by the function pointed out by variable
  462. `dabbrev-friend-buffer-function'.
  463.  
  464. A positive prefix argument, N, says to take the Nth backward *distinct*
  465. possibility.  A negative argument says search forward.
  466.  
  467. If the cursor has not moved from the end of the previous expansion and
  468. no argument is given, replace the previously-made expansion
  469. with the next possible expansion not yet tried.
  470.  
  471. The variable `dabbrev-backward-only' may be used to limit the
  472. direction of search to backward if set non-nil.
  473.  
  474. See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
  475.   (interactive "*P")
  476.   (let (abbrev record-case-pattern
  477.            expansion old direction (orig-point (point)))
  478.     ;; abbrev -- the abbrev to expand
  479.     ;; expansion -- the expansion found (eventually) or nil until then
  480.     ;; old -- the text currently in the buffer
  481.     ;;    (the abbrev, or the previously-made expansion)
  482.     (save-excursion
  483.       (if (and (null arg)
  484.            (markerp dabbrev--last-abbrev-location)
  485.            (marker-position dabbrev--last-abbrev-location)
  486.            (or (eq last-command this-command)
  487.            (and (window-minibuffer-p (selected-window))
  488.             (= dabbrev--last-abbrev-location
  489.                (point)))))
  490.       ;; Find a different expansion for the same abbrev as last time.
  491.       (progn
  492.         (setq abbrev dabbrev--last-abbreviation)
  493.         (setq old dabbrev--last-expansion)
  494.         (setq direction dabbrev--last-direction))
  495.     ;; If the user inserts a space after expanding
  496.     ;; and then asks to expand again, always fetch the next word.
  497.     (if (and (eq (preceding-char) ?\ )
  498.          (markerp dabbrev--last-abbrev-location)
  499.          (marker-position dabbrev--last-abbrev-location)
  500.          (= (point) (1+ dabbrev--last-abbrev-location)))
  501.         (progn
  502.           ;; The "abbrev" to expand is just the space.
  503.           (setq abbrev " ")
  504.           (save-excursion
  505.         (if dabbrev--last-buffer
  506.             (set-buffer dabbrev--last-buffer))
  507.         ;; Find the end of the last "expansion" word.
  508.         (if (or (eq dabbrev--last-direction 1)
  509.             (and (eq dabbrev--last-direction 0)
  510.                  (< dabbrev--last-expansion-location (point))))
  511.             (setq dabbrev--last-expansion-location
  512.               (+ dabbrev--last-expansion-location
  513.                  (length dabbrev--last-expansion))))
  514.         (goto-char dabbrev--last-expansion-location)
  515.         ;; Take the following word, with intermediate separators,
  516.         ;; as our expansion this time.
  517.         (re-search-forward
  518.          (concat "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
  519.         (setq expansion
  520.               (buffer-substring-no-properties dabbrev--last-expansion-location
  521.                     (point)))
  522.         (if dabbrev--last-case-pattern
  523.             (setq expansion (upcase expansion)))
  524.  
  525.         ;; Record the end of this expansion, in case we repeat this.
  526.         (setq dabbrev--last-expansion-location (point)))
  527.           ;; Indicate that dabbrev--last-expansion-location is
  528.           ;; at the end of the expansion.
  529.           (setq dabbrev--last-direction -1))
  530.  
  531.       ;; We have a different abbrev to expand.
  532.       (dabbrev--reset-global-variables)
  533.       (setq direction (if (null arg)
  534.                   (if dabbrev-backward-only 1 0)
  535.                 (prefix-numeric-value arg)))
  536.       (setq abbrev (dabbrev--abbrev-at-point))
  537.       (setq record-case-pattern t)
  538.       (setq old nil)))
  539.  
  540.       ;;--------------------------------
  541.       ;; Find the expansion
  542.       ;;--------------------------------
  543.       (or expansion
  544.       (setq expansion
  545.         (dabbrev--find-expansion abbrev direction
  546.                      (and (eval dabbrev-case-fold-search)
  547.                           (or (not dabbrev-upcase-means-case-search)
  548.                           (string= abbrev (downcase abbrev))))))))
  549.     (cond
  550.      ((not expansion)
  551.       (dabbrev--reset-global-variables)
  552.       (if old
  553.       (save-excursion
  554.         (setq buffer-undo-list (cons orig-point buffer-undo-list))
  555.         ;; Put back the original abbrev with its original case pattern.
  556.         (search-backward old)
  557.         (insert abbrev)
  558.         (delete-region (point) (+ (point) (length old)))))
  559.       (error "No%s dynamic expansion for `%s' found"
  560.          (if old " further" "") abbrev))
  561.      (t
  562.       (if (not (eq dabbrev--last-buffer dabbrev--last-buffer-found))
  563.       (progn
  564.         (message "Expansion found in '%s'"
  565.              (buffer-name dabbrev--last-buffer))
  566.         (setq dabbrev--last-buffer-found dabbrev--last-buffer))
  567.     (message nil))
  568.       (if (and (or (eq (current-buffer) dabbrev--last-buffer)
  569.            (null dabbrev--last-buffer))
  570.            (numberp dabbrev--last-expansion-location)
  571.            (and (> dabbrev--last-expansion-location (point))))
  572.       (setq dabbrev--last-expansion-location
  573.         (copy-marker dabbrev--last-expansion-location)))
  574.       ;; Success: stick it in and return.
  575.       (setq buffer-undo-list (cons orig-point buffer-undo-list))
  576.       (dabbrev--substitute-expansion old abbrev expansion)
  577.  
  578. ;; If we are not copying successive words now,
  579. ;; set dabbrev--last-case-pattern.
  580.       (and record-case-pattern
  581.        (setq dabbrev--last-case-pattern
  582.          (and (eval dabbrev-case-fold-search)
  583.               (not dabbrev-upcase-means-case-search)
  584.               (equal abbrev (upcase abbrev)))))
  585.  
  586.       ;; Save state for re-expand.
  587.       (setq dabbrev--last-expansion expansion)    
  588.       (setq dabbrev--last-abbreviation abbrev)
  589.       (setq dabbrev--last-abbrev-location (point-marker))))))
  590.  
  591. ;;----------------------------------------------------------------
  592. ;; Local functions
  593. ;;----------------------------------------------------------------
  594.  
  595. ;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
  596. (defun dabbrev--same-major-mode-p (other-buffer)
  597.   (eq major-mode
  598.       (save-excursion
  599.     (set-buffer other-buffer)
  600.     major-mode)))
  601.  
  602. ;;; Back over all abbrev type characters and then moves forward over
  603. ;;; all skip characters.
  604. (defun dabbrev--goto-start-of-abbrev ()
  605.   ;; Move backwards over abbrev chars
  606.   (save-match-data
  607.     (if (not (bobp))
  608.     (progn
  609.       (forward-char -1)
  610.       (while (and (looking-at dabbrev--abbrev-char-regexp)
  611.               (not (bobp)))
  612.         (forward-char -1))
  613.       (or (looking-at dabbrev--abbrev-char-regexp)
  614.           (forward-char 1))))
  615.     (and dabbrev-abbrev-skip-leading-regexp
  616.      (while (looking-at dabbrev-abbrev-skip-leading-regexp)
  617.        (forward-char 1)))))
  618.  
  619. ;;; Extract the symbol at point to serve as abbreviation.
  620. (defun dabbrev--abbrev-at-point ()
  621.   ;; Check for error
  622.   (if (bobp)
  623.       (error "No possible abbreviation preceding point"))
  624.   ;; Return abbrev at point
  625.   (save-excursion
  626.     ;; Record the end of the abbreviation.
  627.     (setq dabbrev--last-abbrev-location (point))
  628.     ;; If we aren't right after an abbreviation,
  629.     ;; move point back to just after one.
  630.     ;; This is so the user can get successive words
  631.     ;; by typing the punctuation followed by M-/.
  632.     (save-match-data
  633.       (if (save-excursion
  634.         (forward-char -1)
  635.         (not (looking-at (concat "\\("
  636.                      (or dabbrev-abbrev-char-regexp
  637.                      "\\sw\\|\\s_")
  638.                      "\\)+"))))
  639.       (if (re-search-backward (or dabbrev-abbrev-char-regexp
  640.                       "\\sw\\|\\s_")
  641.                   nil t)
  642.           (forward-char 1)
  643.         (error "No possible abbreviation preceding point"))))
  644.     ;; Now find the beginning of that one.
  645.     (dabbrev--goto-start-of-abbrev)
  646.     (buffer-substring-no-properties dabbrev--last-abbrev-location
  647.               (point))))
  648.     
  649. ;;; Initializes all global variables
  650. (defun dabbrev--reset-global-variables ()
  651.   ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
  652.   ;; must not be reset here.
  653.   (setq dabbrev--last-table nil
  654.     dabbrev--last-abbreviation nil
  655.     dabbrev--last-abbrev-location nil
  656.     dabbrev--last-direction nil
  657.     dabbrev--last-expansion nil
  658.     dabbrev--last-expansion-location nil
  659.     dabbrev--friend-buffer-list nil
  660.     dabbrev--last-buffer nil
  661.     dabbrev--last-buffer-found nil
  662.     dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
  663.                     "\\sw\\|\\s_")
  664.     dabbrev--check-other-buffers dabbrev-check-other-buffers))
  665.  
  666. ;;; Find all buffers that are considered "friends" according to the
  667. ;;; function pointed out by dabbrev-friend-buffer-function.
  668. (defun dabbrev--select-buffers ()
  669.   (save-excursion
  670.     (and (window-minibuffer-p (selected-window))
  671.      (set-buffer (dabbrev--minibuffer-origin)))
  672.     (let ((orig-buffer (current-buffer)))
  673.       (dabbrev-filter-elements
  674.        buffer (buffer-list)
  675.        (and (not (eq orig-buffer buffer))
  676.         (boundp 'dabbrev-friend-buffer-function)
  677.         (funcall dabbrev-friend-buffer-function buffer))))))
  678.  
  679. ;;; Search for ABBREV, N times, normally looking forward,
  680. ;;; but looking in reverse instead if REVERSE is non-nil.
  681. (defun dabbrev--try-find (abbrev reverse n ignore-case)
  682.   (save-excursion
  683.     (save-restriction
  684.       (widen)
  685.       (let ((expansion nil))
  686.     (and dabbrev--last-expansion-location
  687.          (goto-char dabbrev--last-expansion-location))
  688.     (let ((case-fold-search ignore-case)
  689.           (count n))
  690.       (while (and (> count 0)
  691.               (setq expansion (dabbrev--search abbrev
  692.                                reverse
  693.                                ignore-case)))
  694.         (setq count (1- count))))
  695.     (and expansion
  696.          (setq dabbrev--last-expansion-location (point)))
  697.     expansion))))
  698.  
  699. ;;; Find all expansions of ABBREV
  700. (defun dabbrev--find-all-expansions (abbrev ignore-case)
  701.   (let ((all-expansions nil)
  702.     expansion)
  703.     (save-excursion
  704.       (goto-char (point-min))
  705.       (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
  706.     (setq all-expansions (cons expansion all-expansions))))
  707.     all-expansions))
  708.  
  709. (defun dabbrev--scanning-message ()
  710.   (message "Scanning `%s'" (buffer-name (current-buffer))))
  711.  
  712. ;;; Find one occasion of ABBREV.
  713. ;;; DIRECTION > 0 means look that many times backwards.
  714. ;;; DIRECTION < 0 means look that many times forward.
  715. ;;; DIRECTION = 0 means try both backward and forward.
  716. ;;; IGNORE-CASE non-nil means ignore case when searching.
  717. (defun dabbrev--find-expansion (abbrev direction ignore-case)
  718.   (let (expansion)
  719.     (save-excursion
  720.       (cond
  721.        (dabbrev--last-buffer
  722.     (set-buffer dabbrev--last-buffer)
  723.     (dabbrev--scanning-message))
  724.        ((and (not dabbrev-search-these-buffers-only)
  725.          (window-minibuffer-p (selected-window)))
  726.     (set-buffer (dabbrev--minibuffer-origin))
  727.     ;; In the minibuffer-origin buffer we will only search from
  728.     ;; the top and down.
  729.     ;; XEmacs: This is absolutely the stupidest thing I've ever
  730.     ;; heard of.
  731.     ;;(goto-char (point-min))
  732.     ;;(setq direction -1)
  733.     (dabbrev--scanning-message)))
  734.       (cond
  735.        ;; ------------------------------------------
  736.        ;; Look backwards
  737.        ;; ------------------------------------------
  738.        ((and (not dabbrev-search-these-buffers-only)
  739.          (>= direction 0)
  740.          (setq dabbrev--last-direction (min 1 direction))
  741.          (setq expansion (dabbrev--try-find abbrev t
  742.                         (max 1 direction)
  743.                         ignore-case)))
  744.     expansion)
  745.        ;; ------------------------------------------
  746.        ;; Look forward
  747.        ;; ------------------------------------------
  748.        ((and (or (not dabbrev-search-these-buffers-only)
  749.          dabbrev--last-buffer)
  750.          (<= direction 0)
  751.          (setq dabbrev--last-direction -1)
  752.          (setq expansion (dabbrev--try-find abbrev nil
  753.                         (max 1 (- direction))
  754.                         ignore-case)))
  755.     expansion)
  756.        ;; ------------------------------------------
  757.        ;; Look in other buffers.
  758.        ;; Start at (point-min) and look forward.
  759.        ;; ------------------------------------------
  760.        (t
  761.     (setq dabbrev--last-direction -1)
  762.     ;; Make sure that we should check other buffers
  763.     (or dabbrev--friend-buffer-list
  764.         dabbrev--last-buffer
  765.         (setq dabbrev--friend-buffer-list
  766.           (mapcar (function get-buffer)
  767.               dabbrev-search-these-buffers-only))
  768.         (not dabbrev--check-other-buffers)
  769.         (not (or (eq dabbrev--check-other-buffers t)
  770.              (progn
  771.                (setq dabbrev--check-other-buffers
  772.                  (y-or-n-p "Scan other buffers also? ")))))
  773.         (let* (friend-buffer-list non-friend-buffer-list)
  774.           (setq dabbrev--friend-buffer-list
  775.             (funcall dabbrev-select-buffers-function))
  776.           (if dabbrev-check-all-buffers
  777.           (setq non-friend-buffer-list
  778.             (nreverse
  779.              (dabbrev-filter-elements
  780.               buffer (buffer-list)
  781.               (not (memq buffer dabbrev--friend-buffer-list))))
  782.             dabbrev--friend-buffer-list
  783.             (append dabbrev--friend-buffer-list
  784.                 non-friend-buffer-list)))))
  785.     ;; Move buffers that are visible on the screen
  786.     ;; to the front of the list.
  787.     (if dabbrev--friend-buffer-list
  788.         (let ((w (next-window (selected-window))))
  789.           (while (not (eq w (selected-window)))
  790.         (setq dabbrev--friend-buffer-list
  791.               (cons (window-buffer w)
  792.                 (delq (window-buffer w) dabbrev--friend-buffer-list)))
  793.         (setq w (next-window w)))))
  794.     ;; Walk through the buffers
  795.     (while (and (not expansion) dabbrev--friend-buffer-list)
  796.       (setq dabbrev--last-buffer
  797.         (car dabbrev--friend-buffer-list))
  798.       (setq dabbrev--friend-buffer-list
  799.         (cdr dabbrev--friend-buffer-list))
  800.       (set-buffer dabbrev--last-buffer)
  801.       (dabbrev--scanning-message)
  802.       (setq dabbrev--last-expansion-location (point-min))
  803.       (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
  804.     expansion)))))
  805.  
  806. (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
  807.   (if (eq major-mode 'picture-mode)
  808.       (picture-replace-match string fixedcase literal)
  809.     (replace-match string fixedcase literal)))
  810.  
  811. ;;;----------------------------------------------------------------
  812. ;;; Substitute the current string in buffer with the expansion
  813. ;;; OLD is nil or the last expansion substring.
  814. ;;; ABBREV is the abbreviation we are working with.
  815. ;;; EXPANSION is the expansion substring.
  816. (defun dabbrev--substitute-expansion (old abbrev expansion)
  817.   ;;(undo-boundary)
  818.   (let ((use-case-replace (and (eval dabbrev-case-fold-search)
  819.                    (or (not dabbrev-upcase-means-case-search)
  820.                    (string= abbrev (downcase abbrev)))
  821.                    (eval dabbrev-case-replace))))
  822.     (and nil use-case-replace
  823.      (setq old (concat abbrev (or old "")))
  824.      (setq expansion (concat abbrev expansion)))
  825.     ;; If the given abbrev is mixed case and its case pattern
  826.     ;; matches the start of the expansion,
  827.     ;; copy the expansion's case
  828.     ;; instead of downcasing all the rest.
  829.     (if (and (string= abbrev
  830.               (substring expansion 0 (length abbrev)))
  831.          (not (string= abbrev (downcase abbrev)))
  832.          (not (string= abbrev (upcase abbrev))))
  833.     (setq use-case-replace nil))
  834.     (if (equal abbrev " ")
  835.     (setq use-case-replace nil))
  836.     (if use-case-replace
  837.     (setq expansion (downcase expansion)))
  838.     (if old
  839.     (save-excursion
  840.       (search-backward old))
  841.       ;;(store-match-data (list (point-marker) (point-marker)))
  842.       (search-backward abbrev))
  843.     ;; Make case of replacement conform to case of abbreviation
  844.     ;; provided (1) that kind of thing is enabled in this buffer
  845.     ;; and (2) the replacement itself is all lower case.
  846.     (dabbrev--safe-replace-match expansion
  847.                  (not use-case-replace)
  848.                  t)))
  849.  
  850.  
  851. ;;;----------------------------------------------------------------
  852. ;;; Search function used by dabbrevs library.
  853.  
  854. ;;; ABBREV is string to find as prefix of word.  Second arg, REVERSE,
  855. ;;; is t for reverse search, nil for forward.  Variable dabbrev-limit
  856. ;;; controls the maximum search region size.  Third argument IGNORE-CASE
  857. ;;; non-nil means treat case as insignificant while looking for a match
  858. ;;; and when comparing with previous matches.  Also if that's non-nil
  859. ;;; and the match is found at the beginning of a sentence and is in
  860. ;;; lower case except for the initial then it is converted to all lower
  861. ;;; case for return.
  862.  
  863. ;;; Table of expansions already seen is examined in buffer
  864. ;;; `dabbrev--last-table' so that only distinct possibilities are found
  865. ;;; by dabbrev-re-expand.
  866.  
  867. ;;; Value is the expansion, or nil if not found.
  868.  
  869. (defun dabbrev--search (abbrev reverse ignore-case)
  870.   (save-match-data
  871.     (let ((pattern1 (concat (regexp-quote abbrev)
  872.                 "\\(" dabbrev--abbrev-char-regexp "\\)"))
  873.       (pattern2 (concat (regexp-quote abbrev)
  874.                "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
  875.       (found-string nil))
  876.       ;; Limited search.
  877.       (save-restriction
  878.     (and dabbrev-limit
  879.          (narrow-to-region dabbrev--last-expansion-location
  880.                    (+ (point)
  881.                   (if reverse (- dabbrev-limit) dabbrev-limit))))
  882.     ;;--------------------------------
  883.     ;; Look for a distinct expansion, using dabbrev--last-table.
  884.     ;;--------------------------------
  885.     (while (and (not found-string)
  886.             (if reverse
  887.             (re-search-backward pattern1 nil t)
  888.               (re-search-forward pattern1 nil t)))
  889.       (goto-char (match-beginning 0))
  890.       ;; In case we matched in the middle of a word,
  891.       ;; back up to start of word and verify we still match.
  892.       (dabbrev--goto-start-of-abbrev)
  893.  
  894.       (if (not (looking-at pattern1))
  895.           nil
  896.         ;; We have a truly valid match.  Find the end.
  897.         (re-search-forward pattern2)
  898.         (setq found-string
  899.           (buffer-substring-no-properties (match-beginning 1) (match-end 1)))
  900.         (and ignore-case (setq found-string (downcase found-string)))
  901.         ;; Ignore this match if it's already in the table.
  902.         (if (dabbrev-filter-elements
  903.          table-string dabbrev--last-table
  904.          (string= found-string table-string))
  905.         (setq found-string nil)))
  906.       ;; Prepare to continue searching.
  907.       (if reverse
  908.           (goto-char (match-beginning 0))
  909.         (goto-char (match-end 0))))
  910.     ;; If we found something, use it.
  911.     (if found-string
  912.         ;; Put it into `dabbrev--last-table'
  913.         ;; and return it (either downcased, or as is).
  914.         (let ((result
  915.            (buffer-substring-no-properties (match-beginning 0) (match-end 0))))
  916.           (setq dabbrev--last-table
  917.             (cons found-string dabbrev--last-table))
  918.           (if (and ignore-case (eval dabbrev-case-replace))
  919.           ;; XEmacs:  FSF has just `result', which makes absolutely
  920.           ;; no sense in this context
  921.           (downcase result)
  922.         result)))))))
  923.  
  924. (provide 'dabbrev)
  925.  
  926. ;; dabbrev.el ends here
  927.